
    /**  
    * @Title: BaiduLbsApi.java
    * @Package com.baidu.lbs.api
    * @Description: 
    * @author likai
    * @date 2016年9月23日
    * @version V1.0  
    */
    
package com.third.utils.baidu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.supers.exception.ExceptionUtils;
import com.third.base.baidu.AddressInfo;
import com.third.base.baidu.House;
import com.utils.http.ClientUtil;
import com.utils.transtype.TransType;



/**
    * @ClassName: BaiduLbsApi
    * @Description: 百度地图lbs工具
    * @author likai
    * @date 2016年9月23日
    *
    */

public class BaiduLbsApiUtil{
	public static Logger log = LoggerFactory.getLogger(BaiduLbsApiUtil.class);
	private  BaiduLbsConfig baiduLbsConfig;
	private static BaiduLbsApiUtil baiduLbsApi;
	
	private BaiduLbsApiUtil(BaiduLbsConfig baiduLbsConfig) {
		super();
		this.baiduLbsConfig = baiduLbsConfig;
	}

	public static BaiduLbsApiUtil create(BaiduLbsConfig baiduLbsConfig) {
		if (baiduLbsApi==null) {
			baiduLbsApi=new BaiduLbsApiUtil(baiduLbsConfig);
		}
		return baiduLbsApi;
	}
	
	private void publicParam(Map<String, String> param) {
		param.put("geotable_id", baiduLbsConfig.getGeotableId());
		param.put("ak",baiduLbsConfig.getAk());
	}
	
	/* 
	    * @Description:向百度云数据库插入数据
	    * 
	    * @param:
	    * 
	    */
	@SuppressWarnings("unchecked")
	public  Map<String, Object> insertData(AddressInfo addressInfo) {
 		Map<String, String> data=new HashMap<String, String>();
 		publicParam(data);
 		data.put("coord_type", "1");
 		data.put("title", addressInfo.getCmyName());
 		data.put("address", addressInfo.getCmyAddress());
 		data.put("latitude", addressInfo.getCmyLatitude()+"");
 		data.put("longitude", addressInfo.getCmyLongitude()+"");
 		//自定义列
 		data.put("mcvilla_id", addressInfo.getId());//小区id
 		data.put("house_num", "0");//该小区出售房屋总数
 		data.put("rental_house_num", "0");//该小区出租房屋总数
 		data.put("cmy_metro", addressInfo.getCmyMetro());//添加地铁
 		String result=ClientUtil.httpClientPost(data, "http://api.map.baidu.com/geodata/v3/poi/create");
 		return (Map<String, Object>) TransType.jsonToObject(result, Map.class);
	}
 	/* 
	    * @Description:修改百度云数据库数据
	    * 
	    * @param:
	    * 
	    */
	
	
	@SuppressWarnings("unchecked")
	public  Map<String, Object> updateData(AddressInfo addressInfo) {
		Map<String, String> data=new HashMap<String, String>();
		publicParam(data);
		data.put("coord_type", "1");
		data.put("title", addressInfo.getCmyName());
		data.put("address", addressInfo.getCmyAddress());
		data.put("latitude", addressInfo.getCmyLatitude()+"");
		data.put("longitude", addressInfo.getCmyLongitude()+"");
		//自定义列
		data.put("mcvilla_id", addressInfo.getId());//小区id
		String result=ClientUtil.httpClientPost(data, "http://api.map.baidu.com/geodata/v3/poi/update");
		return (Map<String, Object>) TransType.jsonToObject(result, Map.class);
	}
 	
 	/* 
	    * @Description:从百度云数据库删除数据
	    * 
	    * @param:
	    * 
	    */

	@SuppressWarnings("unchecked")
	public  Map<String, Object> deleteData(String id) {
		Map<String, String> data=new HashMap<String, String>();
		publicParam(data);
		data.put("output", "json");
		//自定义列
		data.put("mcvilla_id", id);//小区id
		String result=ClientUtil.httpClientPost(data, "http://api.map.baidu.com/geodata/v3/poi/delete");
		return (Map<String, Object>) TransType.jsonToObject(result, Map.class);
	}
 	
 	/* 
	    * @Description:更新百度云数据库销售房数量
	    * 
	    * @param:
	    * 
	    */
	
	
	@SuppressWarnings("unchecked")
	public  Map<String, Object> updateHouseNum(House house) {
		Map<String, String> data=new HashMap<String, String>();
		publicParam(data);
		//自定义列
		data.put("mcvilla_id", house.getCmyId());//小区id
		data.put("house_num", house.getCmyNUm()+"");//该小区房屋总数
		String result=ClientUtil.httpClientPost(data, "http://api.map.baidu.com/geodata/v3/poi/update");
		return (Map<String, Object>) TransType.jsonToObject(result, Map.class);
	}
	
	
	/* 
	    * @Description:更新百度云数据库出租房数量
	    * 
	    * @param:
	    * 
	    */
	
	
	@SuppressWarnings("unchecked")
	public  Map<String, Object> updateRentalHouseNum(House house) {
		Map<String, String> data=new HashMap<String, String>();
		publicParam(data);
		//自定义列
		data.put("mcvilla_id", house.getCmyId());//小区id
		data.put("rental_house_num", house.getCmyNUm()+"");//该小区房屋总数
		String result=ClientUtil.httpClientPost(data, "http://api.map.baidu.com/geodata/v3/poi/update");
		return (Map<String, Object>) TransType.jsonToObject(result, Map.class);
	}
	
	/* 
	    * @Description:根据id符获取数据
	    * 
	    * @param:
	    * 
	    */
	
	
	@SuppressWarnings("unchecked")
	public  Map<String, Object> getDataById(String id) {
		Map<String, String> data=new HashMap<String, String>();
		publicParam(data);
		//自定义列
		data.put("mcvilla_id", id);//小区id
		String result=ClientUtil.httpClientGet(data, "http://api.map.baidu.com/geodata/v3/poi/detail");
		return (Map<String, Object>) TransType.jsonToObject(result, Map.class);
	}
	
	
	//----------------------------------------------------------------------------百度LBS云数据----------------------------------------------------------------------------------------------------------------------------------
	
	/* 
	    * @Description:条件查询小区
	    * 
	    * @param:
	    * 
	    * 关键词：query
	    * 城市名称：region
	    * 
	    */
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public  List getCommunity(String query,String page_num,String region) {
		List<Object> returnData=new ArrayList<Object>();
		try {
			Map<String, String> data=new HashMap<String, String>();
			data.put("ak",baiduLbsConfig.getAk());
			data.put("query",query);
			data.put("page_size", "10");
			data.put("page_num", page_num);
			data.put("scope", "2");
			data.put("region", region);
			data.put("output", "json");
			data.put("tag","住宅区");
			String returnString=ClientUtil.httpClientGet(data, "http://api.map.baidu.com/place/v2/search");
			Map<String, Object> result=(Map<String, Object>) TransType.fromJson(returnString, Map.class);
			List list=(List) result.get("results");
			for (int i = 0; i < list.size(); i++) {		
				Map<String, String> map=new HashMap<String, String>();
				Map<String, Object> cmy=(Map<String, Object>) list.get(i);
				Map<String, Object> location=(Map<String, Object>)cmy.get("location");
				Map<String, Object> detail_info=(Map<String, Object>)cmy.get("detail_info");
				String cmyname=(String) cmy.get("name");//小区名
				String address=(String) cmy.get("address");//详细地址
				Double lat=(Double) location.get("lat");//纬度
				Double lng=(Double) location.get("lng");//经度
				String price=(String) detail_info.get("price");//均价
				map.put("cmyname", cmyname);
				map.put("address", address);
				map.put("lat", lat+"");
				map.put("lng", lng+"");
				map.put("price", price==null?"0":price);
				returnData.add(map);
			}
		} catch (Exception e) {
			log.error(ExceptionUtils.getExceptionMessage(e));
		}
		return returnData;
		
	}

	/* 
	    * @Description:根据百度经纬度获取地理位置信息
	    * 
	    * @param:
	    * latitude:纬度
	    * longitude：经度
	    */
	
	
	@SuppressWarnings("unchecked")
	public  Map<String, String> getAddressByCoord(String latitude,String longitude) {
		Map<String, String> returnData=new HashMap<String, String>();
		try {
			Map<String, String> data=new HashMap<String, String>();
			data.put("ak", baiduLbsConfig.getAk());
			data.put("location", latitude+","+longitude);
			data.put("pois","0");
			data.put("output", "json");
			String returnSting=ClientUtil.httpClientGet(data, "http://api.map.baidu.com/geocoder/v2/");
			Map<String, Object> result=(Map<String, Object>) TransType.fromJson(returnSting, Map.class);
			Map<String, Object> m=(Map<String, Object>) result.get("result");
			Map<String, Object> m1=(Map<String, Object>) m.get("addressComponent");
			
			String formatted_address=(String) m.get("formatted_address");//详细地址
			String country=(String) m1.get("country");//国
			String province=(String) m1.get("province");//省
			String city=(String) m1.get("city");//市
			String district=(String) m1.get("district");//区
			returnData.put("address",formatted_address );
			returnData.put("country", country);
			returnData.put("province", province);
			returnData.put("city", city);
			returnData.put("district", district);//区县
		} catch (Exception e) {
			log.error(ExceptionUtils.getExceptionMessage(e));
		}
		return returnData;
		
	}
	
	/* 
	    * @Description:根据地理位置获取中心经纬度
	    * 
	    * @param:
	    * 城市名：city
	    */
	
	
	@SuppressWarnings("unchecked")
	public  Map<String, String> getCoordByAddress(String address,String city) {
		Map<String, String> returnData=new HashMap<String, String>();
		try {
			Map<String, String> data=new HashMap<String, String>();
			data.put("ak", baiduLbsConfig.getAk());
			data.put("address", address);
			data.put("city",city);
			data.put("output", "json");
			String returnSting=ClientUtil.httpClientGet(data, "http://api.map.baidu.com/cloudgc/v1");
			Map<String, Object> result=(Map<String, Object>) TransType.fromJson(returnSting, Map.class);
			List<Map<String, Object>>  m=(List<Map<String, Object>>) result.get("result");
			Map<String, Object> m1=(Map<String, Object>) m.get(0).get("location");	
			String lat= m1.get("lat")+"";//中心纬度
			String lng= m1.get("lng")+"";//中心经度	
			returnData.put("lat", lat);
			returnData.put("lng", lng);
		} catch (Exception e) {
			log.error(ExceptionUtils.getExceptionMessage(e));
		}
		return returnData;
		
	}
	public static void main(String[] args) {
		//System.err.println(getCoordByAddress("上海","上海"));
	}

	/* 
	    * @Description:根据百度经纬度获取地铁信息
	    * 
	    * @param:
	    * latitude:纬度
	    * longitude：经度
	    */
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public  String getMetroByCoord(String latitude,String longitude) {
		HashSet<String> returnSet=new HashSet<String>();
		try {
			Map<String, String> data=new HashMap<String, String>();
			data.put("ak", baiduLbsConfig.getAk());
			data.put("location", latitude+","+longitude);
			data.put("radius","2000");//方圆2000米
			data.put("output", "json");
			data.put("scope", "2");
			data.put("page_size", "10");
			data.put("page_num", "0");
			data.put("query", "地铁");
			String returnSting=ClientUtil.httpClientGet(data, "http://api.map.baidu.com/place/v2/search");
			Map<String, Object> result=(Map<String, Object>) TransType.fromJson(returnSting, Map.class);
			List list=(List) result.get("results");
			Map<String, String> map1=new HashMap<String, String>();
			Map<String, Double> map2=new HashMap<String, Double>();
			for (int i = 0; i < list.size(); i++) {		
				Map<String, Object> metro=(Map<String, Object>) list.get(i);
				Map<String, Object> detail_info=(Map<String, Object>)metro.get("detail_info");
				String name=(String) metro.get("name");//站名
				String address=(String) metro.get("address");//线路
				Double distance=(Double) detail_info.get("distance");//距离
				if (address!=null) {					
					if (map1.get(address)==null) {
						map1.put(address, name);
						map2.put(address, distance);
					}else {
						if (map2.get(address)>distance) {
							map1.put(address, name);
							map2.put(address, distance);
						}
					}
					Set<String> keyset=map1.keySet();
					String key=keyset.iterator().next();
					//最多拿取5条路线
					if (keyset.size()<5) {					
						for (int j = 0; j < keyset.size(); j++) {					
							returnSet.add(key+" "+"距离"+map1.get(key)+"站"+map2.get(key)+"米");
						}
					}else {
						for (int j = 0; j <5; j++) {					
							returnSet.add(key+" "+"距离"+map1.get(key)+"站"+map2.get(key)+"米");
						}
					}
				}
			}
		} catch (Exception e) {
			log.error(ExceptionUtils.getExceptionMessage(e));
			return "";

		}
		return returnSet.toString().replaceAll("[\\[\\]]", "");
		
	}
	
	
}
